#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/6/15 16:20
# @Author  : fuchen'en
# @Description:
import pika
import sys

credentials = pika.PlainCredentials('test', 'test123')
parameters = pika.ConnectionParameters(host='10.0.20.66',
                                       port='5672',
                                       virtual_host='/',
                                       credentials=credentials)
connection = pika.BlockingConnection(parameters)
channel = connection.channel()

# 主题型交换机是最强交换机，可以包含前面广播、直连交换机的功能
# 主题型交换机可以根据routing_key，实现批量和分组匹配
# 通配符有'#'和'*',前者匹配任意长度的字符串，后者匹配一个单词
channel.exchange_declare(exchange='topic_logs', exchange_type='topic')

routing_key = sys.argv[1] if len(sys.argv) > 2 else 'anonymous info'
message = ' '.join(sys.argv[2:]) or 'Hello World!'
channel.basic_publish(exchange='topic_logs', routing_key=routing_key, body=message)
print(' [x] Sent {!r}: {!r}'.format(routing_key, message))
connection.close()
